[ Vue筆記 ] Vue使用setInterval


Posted by Akira on 2022-02-20

前言

最近在寫番茄鐘的Side project,在使用 setInterval 做計時器時,遇到了小小問題。
這邊紀錄一下解法。

範例

  data() {
    return {
      time: null,
    };
  },
  methods: {
    timer() {
      this.time = setInterval(
        function () {
        // 想重複的動作
        }.bind(this),
        200
      );
    },
    setTime() {
      this.timer();
    },
    stopTime() {
      if (this.time) {
        clearInterval(this.time);
        this.time = null;
      }
    },
}









Related Posts

2021 區塊鏈開發入門

2021 區塊鏈開發入門

OOP 13 - Dependency Inversion Principle

OOP 13 - Dependency Inversion Principle

搜尋資料庫所有欄位

搜尋資料庫所有欄位


Comments